package com.polytech.web.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.polytech.web.client.model.Person;
import com.polytech.web.client.service.PersonService;
import com.polytech.web.shared.Query;

public class PersonServiceImpl extends RemoteServiceServlet implements
		PersonService {

	private static final long serialVersionUID = 1L;

	private List<Person> userList = new ArrayList<Person>();

	public PersonServiceImpl() {
	}

	public  Person getUser(String nameUser, String lastname) {
		Query query = new Query();
		return query.findPersonByName(nameUser, lastname);
	}

	public List<Person> getUserList() {
		return userList;
	}

	@Override
public List<Person> getUserList(String criteria, String name) {
	Query query = new Query();
	
	if(criteria.equals("Friend")){
		//if(query.findPersonByName(name)==null){
			//return null;
		//}else{
			return query.getFriends(name);
		//}
		
	}
	
	if(criteria.equals("Country")){
		//if(query.findPersonByName(name)==null){
			//return null;
		//}else{
			return query.getCountry(name);
		//}
	}
	
	if(criteria.equals("Family")){
		/*if(query.findPersonByName(name)==null){
			return null;
		}else{*/
			return query.getFamily(name);
		//}
	}
	if(criteria.equals("Classmate")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			return query.getClassmate(name);
		//}
	}
	
	if(criteria.equals("Colleague")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			return query.getColleague(name);
		//}
	}
	
	if(criteria.equals("Interest")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			return query.getTopicInterest(name);
		//}
	}
	
	if(criteria.equals("Groupe")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			return query.getGroupe(name);
		//}
	}
	
	if(criteria.equals("Mail Groupe")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			return query.getMailGroupe(name);
		//}
	}
	if(criteria.equals("Anniversaire")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			return query.getAnniversaire(name);
		//}
	}
	
	if(criteria.equals("Generation")){
		/*if(query.findPersonByName(name)==null){
		return null;
	}else{*/
			int age = Integer.valueOf(query.getAge(name));
			int age1 = age+10;
			int age2;
			if(age>10){
				age2 = age-10;
			}else{
				age2 = age+10;
			}
			return query.getAmisPlageAge(name, String.valueOf(age1), String.valueOf(age2));
		//}
	}
	return null;
}

	
	public Person addFriend(String username, String friendname) {
		// TODO Auto-generated method stub
		Query query = new Query();
		try {
			return query.addFriendSuggestion(username, friendname);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
		//return null;
	}
}
